//
//  UIButton+YYAdd.m
//  Commom
//
//  Created by 山刘 on 2019/3/28.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "UIButton+YYAdd.h"
#import <objc/runtime.h>

static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;

@implementation UIButton (YYAdd)
+(UIButton*)buttonWithFrame:(CGRect)frame
                  TitleFont:(UIFont*)font
                        Tag:(NSInteger)tag
                TitleNormal:(NSString*)titleNormal
              TitleSelected:(NSString*)titleSelected
            BackgroundColor:(UIColor*)backgroundColor
           TitleColorNormal:(UIColor*)titleColorNormal
         TitleColorSelected:(UIColor*)titleColorSelected
      BackgroundImageNormal:(UIImage*)backgroundImageNormal
    BackgroundImageSelected:(UIImage*)backgroundImageSelected
                ImageNormal:(UIImage*)imageNormal
              ImageSelected:(UIImage*)imageSelected
{
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setFrame:frame];
    
    [btn.titleLabel setFont:font];
    
    if (tag > 0) {
        [btn setTag:tag];
    }
    
    if (backgroundColor) {
        [btn setBackgroundColor:backgroundColor];
    }
    
    if (titleNormal) {
        [btn setTitle:titleNormal forState:UIControlStateNormal];
    }
    
    if (titleSelected) {
        [btn setTitle:titleSelected forState:UIControlStateSelected];
    }
    
    if (titleColorNormal) {
        [btn setTitleColor:titleColorNormal forState:UIControlStateNormal];
    }
    
    if (titleColorSelected) {
        [btn setTitleColor:titleColorSelected forState:UIControlStateSelected];
    }
    
    if (backgroundImageNormal) {
        [btn setBackgroundImage:backgroundImageNormal  forState:UIControlStateNormal];
    }
    
    if (backgroundImageSelected) {
        [btn setBackgroundImage:backgroundImageSelected  forState:UIControlStateSelected];
    }
    
    if (imageNormal) {
        [btn setImage:imageNormal forState:UIControlStateNormal];
    }
    
    if (imageSelected) {
        [btn setImage:imageSelected forState:UIControlStateSelected];
    }
    
    return btn;
}

- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left {
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect)enlargedRect {
    NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
    NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge) {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    } else {
        return self.bounds;
    }
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
    CGRect rect = [self enlargedRect];
    //如果按钮设置为不可点击、隐藏、透明度小于等于0.01或者点击在按钮内部，则直接执行父类方法
    if (CGRectEqualToRect(rect, self.bounds) || self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
        return [super hitTest:point withEvent:event];
    }
    //判断点击是否在放大的范围内
    return CGRectContainsPoint(rect, point) ? self : nil;
}
@end
